package com.jtlrm.ckd.mvp.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.base.sdk.base.net.CommonObserver;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jtlrm.ckd.R;
import com.jtlrm.ckd.base.acitvity.TitleBarActivity;
import com.jtlrm.ckd.config.LiangBiaoName;
import com.jtlrm.ckd.config.UrlConstants;
import com.jtlrm.ckd.entity.LiangBiaoEntity;
import com.jtlrm.ckd.entity.PageEntity;
import com.jtlrm.ckd.entity.event.ActivityFinishMessage;
import com.jtlrm.ckd.entity.event.YinShiUpdateMessage;
import com.jtlrm.ckd.mvp.model.LiangBiaoModel;
import com.jtlrm.ckd.mvp.view.adapter.LiangBiaoAdapter;
import com.jtlrm.ckd.mvp.view.adapter.NewsAdapter;
import com.jtlrm.ckd.util.EventBusUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;

/**
 * 量表评估子界面
 */
public class LiangBiaoPingGuSubListActivity extends TitleBarActivity {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    private int pageNum = 1;
    private int pageSize = 12;
    LiangBiaoAdapter adapter;
    LiangBiaoModel liangBiaoModel;
    String patientId;
    String type;
    String title;

    public static void goDetail(Context context, String patientId, String type, String title) {
        Intent intent = new Intent(context, LiangBiaoPingGuSubListActivity.class);
        intent.putExtra("patientId", patientId);
        intent.putExtra("type", type);
        intent.putExtra("title", title);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Intent intent = getIntent();
        patientId = intent.getStringExtra("patientId");
        type = intent.getStringExtra("type");
        title = intent.getStringExtra("title");
        super.onCreate(savedInstanceState);
    }

    @Override
    protected String getTitleText() {
        return title;
    }

    @Override
    protected void setContentLayout() {
        setContentView(R.layout.activity_liang_biao_ping_gu_sub_list);
    }

    @Override
    protected void initView() {
        adapter = new LiangBiaoAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.addItemDecoration(createItemDecoration());
        recyclerView.setAdapter(adapter);
        liangBiaoModel = new LiangBiaoModel();
        titleBar.tvRight.setText("添加评估");
        titleBar.tvRight.setVisibility(View.VISIBLE);
        EventBusUtil.register(this);
    }

    @Override
    protected void obtainData() {

    }

    @Override
    protected void initEvent() {
        adapter.openLoadAnimation();
        smartRefreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pageNum++;
                loadData();
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageNum = 1;
                loadData();
            }
        });
        smartRefreshLayout.autoRefresh();
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                LiangBiaoEntity entity = (LiangBiaoEntity) adapter.getData().get(position);
                switch (type) {
                    case LiangBiaoName.SGA:
                        LiangBiaoDetailActivity.goDetail(context, UrlConstants.SGA + "id=" + entity.getId(), title);
                        break;
                    case LiangBiaoName.MIS:
                        LiangBiaoDetailActivity.goDetail(context, UrlConstants.MIS + "id=" + entity.getId(), title);
                        break;
                    case LiangBiaoName.NRS:
                        LiangBiaoDetailActivity.goDetail(context, UrlConstants.NRS + "id=" + entity.getId(), title);
                        break;
                    case LiangBiaoName.MNA:
                        LiangBiaoDetailActivity.goDetail(context, UrlConstants.MNA + "id=" + entity.getId(), title);
                        break;
                    case LiangBiaoName.GLSGA:
                        LiangBiaoDetailActivity.goDetail(context, UrlConstants.GLSGA + "id=" + entity.getId(), title);
                        break;
                }
            }
        });
        titleBar.tvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (type) {
                    case LiangBiaoName.SGA:
                        LiangBiaoDetailActivity.goDetail(context, UrlConstants.SGA + "pid=" + patientId, title);
                        break;
                    case LiangBiaoName.MIS:
                        LiangBiaoDetailActivity.goDetail(context, UrlConstants.MIS + "pid=" + patientId, title);
                        break;
                    case LiangBiaoName.NRS:
                        LiangBiaoDetailActivity.goDetail(context, UrlConstants.NRS + "pid=" + patientId, title);
                        break;
                    case LiangBiaoName.MNA:
                        LiangBiaoDetailActivity.goDetail(context, UrlConstants.MNA + "pid=" + patientId, title);
                        break;
                    case LiangBiaoName.GLSGA:
                        LiangBiaoDetailActivity.goDetail(context, UrlConstants.GLSGA + "pid=" + patientId, title);
                        break;
                }
            }
        });

    }

    private void loadData() {
        liangBiaoModel.queryHuanZheList(pageNum, pageSize, patientId, type, new CommonObserver<PageEntity<List<LiangBiaoEntity>>>() {
            @Override
            public void onError(int errType, String errMessage) {
                showToast(errMessage);
                closeFrshorLoadmore();
            }

            @Override
            public void onResult(PageEntity<List<LiangBiaoEntity>> data) {
                closeFrshorLoadmore();
                if (pageNum > 1) {
                    adapter.addData(data.getItems());
                } else {
                    adapter.replaceData(data.getItems());
                }
                if (data.getItems().size() < pageSize) {
                    smartRefreshLayout.setEnableLoadmore(false);
                } else {
                    smartRefreshLayout.setEnableLoadmore(true);
                }
            }
        }, lifecycleSubject);
    }


    /**
     * 关闭加载更多或者刷新
     */
    private void closeFrshorLoadmore() {
        if (smartRefreshLayout.isLoading()) {
            smartRefreshLayout.finishLoadmore();
        }
        if (smartRefreshLayout.isRefreshing()) {
            smartRefreshLayout.finishRefresh();
        }
    }

    protected RecyclerView.ItemDecoration createItemDecoration() {
        return new DefaultItemDecoration(ContextCompat.getColor(this, R.color.line_color), 2, 1, -1);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(ActivityFinishMessage message) {
        if (message != null) {
            loadData();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBusUtil.unregister(this);
    }
}
